import numpy as np
import matplotlib.pyplot as plt

def B(i, x):
    if np.any(x > i-1) and np.any(x <= i):
        return 1.0*(x-i+1)*(x-i+1)/2
    elif np.any(x > i) and np.any(x <= i+1):
        return 1.0*((x-i+1)*(i+1-x)+(i+2-x)*(x-i))/2
    elif np.any(x > i+1) and np.any(x <= i+2):
        return 1.0*(i+2-x)*(i+2-x)/2
    else:
        return 0

x = np.linspace(-1, 2, 200)

for i in range(-2, 2, 1):
    y = []
    z = []
    for k in x:
        y.append(B(i, k))
        z.append(1.0)
    plt.plot(x, y)
    plt.plot(x,z,"--")

plt.savefig("5_e.png")
